package com.liangchao.blogo.web;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.liangchao.blogo.Ingredient;
import com.liangchao.blogo.Taco;
import com.liangchao.blogo.Ingredient.Type;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Controller
@RequestMapping("/design")
public class DesignTaocoController {
    @GetMapping
    public String showDesignForm(Model model) {
        List<Ingredient> ingredients = Arrays.asList(
            new Ingredient("FLTO", "Flour Tortilla", Type.WRAP),
            new Ingredient("COTO", "Corn Tortilla", Type.WRAP),
            new Ingredient("CARN", "Carnitas", Type.PROTEIN),
            new Ingredient("TMTO", "Diced Tomatoes", Type.PROTEIN));

        Type[] types = Ingredient.Type.values();
        System.out.println(Arrays.toString(types));
        for (Type type : types) {
            model.addAttribute(type.toString(), filterByType(ingredients, type));
        }

        model.addAttribute("design", new Taco());
        return "design";
    }

    @PostMapping
    public String processDesign(Taco design) {
        // Save the taco design..
        // We'll do the in chapteer3
        log.info("Processing design: " + design);
        return "redirect:/orders/current";
    }
    
    private List<Ingredient> filterByType(List<Ingredient> ingredients, Type type) {
        return ingredients
            .stream()
            .filter(x -> x.getType().equals(type))
            .collect(Collectors.toList());
    }
}
